package cn.bugstack.springframework.beans.core.io;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
//通过 HTTP 的方式读取云服务的文件，我们也可以把配置文件放到 GitHub 或者Gitee 上。
public class UrlResource implements Resource{
    private final URL url;
    public UrlResource(URL url){
        this.url=url;
    }
    @Override
    public InputStream getInputStream() throws IOException {
        URLConnection con=this.url.openConnection();
        try{
            return con.getInputStream();
        }catch (IOException ex){
            if (con instanceof HttpURLConnection){
                ((HttpURLConnection)con).disconnect();
            }
            throw ex;
        }
    }
}
